package com.abso.mp3tunes.locker.ui.actions;

import java.io.IOException;
import java.io.PrintWriter;

import org.apache.commons.lang.SystemUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.eclipse.core.runtime.IProgressMonitor;

import com.abso.mp3tunes.locker.core.data.Track;
import com.abso.mp3tunes.locker.ui.LockerImages;
import com.abso.mp3tunes.locker.ui.viewers.LockerViewer;

/**
 * Action able to export a Media Player ASX playlist. More precisely, the
 * correct format should be WAX, but the extension ASX is more popular and
 * therefore we use it.
 */
public class ExportASXPlaylistAction extends AbstractExportPlaylistAction {

	/**
	 * Constructs a new action.
	 * 
	 * @param lockerViewer
	 *            the Locker tree viewer.
	 */
	public ExportASXPlaylistAction(LockerViewer lockerViewer) {
		super("ASX", LockerImages.getDescriptor(LockerImages.ASX), lockerViewer);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.abso.mp3tunes.locker.ui.actions.AbstractExportPlaylistAction#getFileFilterExtensions()
	 */
	protected String[] getFileFilterExtensions() {
		return new String[] { "*.asx", "*.wsx" };
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.abso.mp3tunes.locker.ui.actions.AbstractExportPlaylistAction#getFileEncoding()
	 */
	protected String getFileEncoding() {
		return "UTF-8";
	}

	/* (non-Javadoc)
	 * @see com.abso.mp3tunes.locker.ui.actions.AbstractExportPlaylistAction#getPlaylistPath(java.lang.String)
	 */
	protected String getPlaylistPath(String filePath) {
		String playlistPath = filePath;
		if (!playlistPath.toLowerCase().endsWith(".asx") && !playlistPath.toLowerCase().endsWith(".wsx")) {
			return playlistPath + ".asx";
		} else {
			return playlistPath;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.abso.mp3tunes.locker.ui.actions.AbstractExportPlaylistAction#exportTracks(com.abso.mp3tunes.locker.core.data.Track[],
	 *      java.io.PrintWriter, org.eclipse.core.runtime.IProgressMonitor)
	 */
	protected void exportTracks(Track[] tracks, PrintWriter writer,
			IProgressMonitor monitor) throws IOException {
		Document doc = DocumentHelper.createDocument();
		Element asx = doc.addElement("ASX").addAttribute("VERSION", "3.0");
		asx.addText(SystemUtils.LINE_SEPARATOR);
		asx.addElement("PARAM").addAttribute("NAME", "Encoding").addAttribute("VALUE", "utf-8");
		asx.addText(SystemUtils.LINE_SEPARATOR);
		asx.addElement("TITLE").setText("MP3tunes Locker");
		asx.addText(SystemUtils.LINE_SEPARATOR);
		for (int i = 0; i < tracks.length; i++) {
			if (monitor.isCanceled()) {
				break;
			}
			Element entry = asx.addElement("ENTRY");
			entry.addElement("REF").addAttribute("HREF", tracks[i].getPlayURL());
			entry.addElement("TITLE").setText(tracks[i].getArtistName() + " - " + tracks[i].getAlbumTitle() + " - " + tracks[i].getTitle());
			asx.addText(SystemUtils.LINE_SEPARATOR);
			monitor.worked(1);
		}
        OutputFormat outFormat = new OutputFormat();
        outFormat.setSuppressDeclaration(true);
        outFormat.setIndent(false);
        outFormat.setNewlines(false);
        outFormat.setTrimText(false);
        outFormat.setPadText(false);
        outFormat.setEncoding("UTF-8");
        XMLWriter xmlWriter = new XMLWriter(writer, outFormat);
        xmlWriter.setMaximumAllowedCharacter(127);
        xmlWriter.write(doc);
        xmlWriter.flush();
        xmlWriter.close();
	}

}
